/*
 * Copyright (C) 2011 : Gilles Coomans 
 * 
 * This file is part of QuintEssence.
 * QuintEssence is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * QuintEssence is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with QuintEssence.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.exprom.qe;
import net.exprom.qe.fields.QELeaf;
import Type;

/**
 * ...ajout : {longitude, latitude}, {x:0,y:0}, {x:0,y:0,z:0}
 * @author Gilles Coomans
 */

class QETypeValidator 
{
	public var errors:Array<Dynamic>;
	public function new() 
	{
		errors = [];
	}
	public function clearErrors():Void
	{
		errors = [];
	}
	
	public function validate(f:QELeaf):Bool
	{
		//trace("Validator : " + f);
		if (Reflect.isFunction(f._value) || f.parameters.valueType == null || f._value == null || !f.setted)
			return true;
		
		if (Std.is(f.parameters.valueType, String))
		{
			switch(f.parameters.valueType)
			{
				case "STRING":
					if(Std.is(f._value, String))
						return true;
					errors.push( { field:f, msg:("Needing STRING value for field : "+f.path + " - value : "+f._value ) } );
					return false;
				case "FLOAT":
					if(Std.is(f._value, Float))
						return true;
					errors.push( { field:f, msg:("Needing FLOAT value for field : "+f.path + " - value : "+f._value ) } );
					return false;
				case "INT":
					if(Std.is(f._value, Int))
						return true;
					errors.push( { field:f, msg:("Needing INT value for field : "+f.path + " - value : "+f._value ) } );
					return false;
				case "EMAIL":
					var eregEmail = ~/^[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[@]{1}[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[.]{1}[A-Za-z]{2,5}$/ig;
					if (eregEmail.match(f._value))
						return true;
					errors.push( { field:f, msg:("Needing EMAIL value for field : "+f.path + " - value : "+f._value ) } );
					return false;
				case "DATE":
					if (Std.is(f._value, String) && Date.fromString(f._value) != null)
						return true;
					if (Std.is(f._value, Float) && Date.fromTime(f._value) != null)
						return true;
					errors.push( { field:f, msg:("Needing DATE (format : 'AAAA-MM-JJ HH:MM:SS' or 'AAAA-MM-JJ' or 'HH:MM:SS' or timestamp) value for field : "+f.path + " - value : "+f._value ) } );
					return false;
				case "DYNAMIC":
					return true;
				case "FREENODE":
					if(Type.typeof(f._value) ==  TObject)
						return true;
					errors.push( { field:f, msg:("Needing FREENODE (anonymous) value for field : "+f.path + " - value : "+f._value ) } );
					return false;
				case "ARRAY" :
					if(Std.is(f._value, Array))
						return true;
					errors.push( { field:f, msg:("Needing ARRAY value for field : "+f.path + " - value : "+f._value ) } );
					return false;
			}
		}else if (Std.is(f.parameters.valueType, EReg))
		{
			if (f.parameters.valueType.match(f._value))
				return true;
			errors.push( { field:f, msg:("Needing EReg value ( "+Std.string(f.parameters.valueType)+" ) for field : "+f.path + " - value : "+f._value ) } );
			return false;
		}
		return true;
	}
	
}